//
//  MASExampleScrollHorView.m
//  MasonryExample
//
//  Created by 山刘 on 2020/6/10.
//  Copyright © 2020 山刘. All rights reserved.
//

#import "MASExampleScrollHorView.h"
static UIEdgeInsets padding;
@implementation MASExampleScrollHorView

- (id)init {
    self = [super init];
    if (!self) return nil;
    padding = UIEdgeInsetsMake(10, 10, 10, 10);
    
    UIScrollView *scrollView = [LayoutUtils creatUIScrollView];
    [self addSubview:scrollView];
    [scrollView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(padding); // make.edges.equalTo(self).mas_equalTo(padding);
    }];
    
    // 设置scrollView的子视图，即过渡视图contentSize
    UIView* contentView = [LayoutUtils createView];
    [scrollView addSubview:contentView];
    [contentView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(scrollView);
        make.height.equalTo(scrollView);
    }];
    
    // 动态添加子视图到 contentView 上
    UIView *lastView = nil;
    for (int i = 0; i <= 10; i++) {
        UILabel *label = [LayoutUtils fixedLabelWithText:[NSString stringWithFormat:@"水平方向\n第 %d 个视图", (i + 1)]];
        [contentView addSubview:label];
        [label makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.mas_equalTo(padding);
            make.width.equalTo(scrollView).offset(-(padding.left+padding.right));
            if (lastView) {
                make.left.mas_equalTo(lastView.mas_right).offset(padding.left+padding.right);
            } else {
                make.left.mas_equalTo(padding);
            }
        }];
        lastView = label;
    }
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(lastView.mas_right).offset(padding.left+padding.right);
    }];
    
    return self;
}

@end
